"Strings as Arrays" Utilities

Indexed string

Retrieves the Nth substring from a string. Substrings are delimited by colons and counted from index zero.

// Find the substring at index n, counting 0 to n
function doIndex(aString, n)
{
	
	var str=""+aString
	
	// Count until the correct index
	for(var i = 0; i < n; i++)
	{
		var where = str.indexOf(':', 1)
	    str = str.substring(where+1, str.length)
	}

	// Lop off the end of the string
	return str.substring(0, str.indexOf(':'))
}

Indexed strings as Numbers

Retrieves the Nth number (integer) from a string. Indices start at zero.

// Find the substring at index n, counting 0 to n
function indexNumber(aString, n)
{
	return parseInt(doIndex(aString, n))
}
Copyright ©1998 by Charles River Media, All Rights Reserved